//
//  ViewController.m
//  BlockMenuController
//
//  Created by 郑东喜 on 2017/3/24.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

#import "ViewController.h"

#import <WebKit/WebKit.h>

@interface ViewController () {
    WKWebView *webview;
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    
    
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    
    
    // 禁止选择CSS
    NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";

    
    // CSS选中样式取消
    NSMutableString *javascript = [NSMutableString string];
    [javascript appendString:@"var style = document.createElement('style');"];
    [javascript appendString:@"style.type = 'text/css';"];
    [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
    [javascript appendString:@"style.appendChild(cssContent);"];
    [javascript appendString:@"document.body.appendChild(style);"];
    
    
    // javascript注入
    WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    
    [userContentController addUserScript:noneSelectScript];
    
    configuration.userContentController = userContentController;

    
    // WKWebView 初始化
    webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
    
    NSURL *url = [[NSURL alloc] initWithString:@"http://baike.baidu.com/link?url=ouLDOM3w0X5RyNDaOPPvREhRkHSGh00tf4qDQ4kmrUvu8muMLvLlhRtgHTfgGOCA5Tx8FCrtZ-0ztmvQAq5BHhc7QOAIE6FNwSKDZ2Slu1G"];
    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];
    [webview loadRequest:request];
    
    [self.view addSubview:webview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
